const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId: '', // 活动id
    receivingInstructions: '', // 领取说明
    id: '', // 用户id
    // 以下数据从领取奖品接口获得
    data: {
      'id': '', // 用户id
      'teamExchangeCode': '', // 组队兑换码
      personalExchangeCode: '', // 个人兑换码
      'consignee': '', // 收件人
      'telephone': '', // 手机号
      'address': '', // 详细地址 
      'province': '', // 省
      'city': '', // 市
      'area': '', // 区
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.activityId = app.globalData.activityData.activityId
    const { id } = options
    this.data.id = id
    wx.showLoading({
      title: '加载中'
    })
    Promise.all([
      this.getElements(),
      this.getPrizeCollection()
    ]).then(() => {
      wx.hideLoading()
    })
  },
  /**
   * @see 获取领取说明
   */
  getElements () {
    return new Promise(resolve => {
      const { activityId } = this.data
      const request = {
        activityId,
        eIndex: [7]
      }
      wx.$API['eventConfiguration/getElements'](request).then(res => {
        if (res.data.length === 0) {
          wx.showToast({ title: '暂无数据', icon: 'none' })
          return
        }
        const config = JSON.parse(res.data[0].content)
        this.setData({
          receivingInstructions: config.receivingInstructions
        })
        resolve()
      })
        .catch(error => {
          wx.showToast({
            title: error,
            icon: 'none'
          })
          console.log('eventConfiguration/getElements 获取元素错误!', error);
          resolve()
        })
    })
  },
  /**
   * @see 领取奖品
   */
  getPrizeCollection () {
    return new Promise(resolve => {
      const { id } = this.data
      // let { activityId } = app.globalData.activityData
      const request = {
        activityId: this.data.activityId,
        id
      }
      wx.$API['eventConfiguration/getPrizeCollection'](request)
        .then(res => {
          this.setData({
            data: res.data[0]
          })
          resolve()
        })
        .catch(error => {
          wx.showToast({
            title: error,
            icon: 'none'
          })
          console.log('eventConfiguration/getPrizeCollection 错误!', error, '参数为:', request)
          resolve()
        })
    })
  },
  /**
   * @see 复制兑换码
   */
  copyCode (e) {
    const { data } = this.data
    wx.setClipboardData({
      data: data[e.currentTarget.dataset.name] + '',
    })
  },
})